<?php

namespace app\admin\Controller;


use think\Controller;
use think\Session;
use think\Db;

class Auth extends Controller
{

    public function login()
    {
        if (is_login(Session::get('admin'))) {
            $this->success('已登录，无需重复登录', '/admin/index/index');
        }
        return $this->fetch();
    }

    public function logout()
    {
//        Session::clear();
        Session::delete('admin');
        return redirect(('/Admin/Auth/login'));
    }


    public function check_user()
    {
        $data = $this->request->post();
        if ($data) {
            $res = Db::table('admin')
                ->where([
                    'username' => $data['username'],
                    'password' => md5($data['password']),
                    'state' => 1,
                    'role_id' => 0
                ])->find();
            if ($res) {
                Session::set('admin.id', $res['id']);
                Session::set('admin.username', $res['username']);
                if (Session::get('admin')) {
                    return json(['msg' => '登录成功', 'code' => 0]);
                } else {
                    return json(['msg' => '登录失败', 'code' => 1]);
                }
            } else {
                return json(['msg' => '用户名或者密码错误!', 'code' => 1]);
            }
        }
    }

}
